package com.example.demo.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.request.AlipayMerchantItemFileUploadRequest;
import com.alipay.api.request.AlipayOpenAuthTokenAppRequest;
import com.alipay.api.request.AntMerchantExpandItemOpenCreateRequest;
import com.alipay.api.response.AlipayMerchantItemFileUploadResponse;
import com.alipay.api.response.AlipayOpenAuthTokenAppResponse;
import com.alipay.api.response.AntMerchantExpandItemOpenCreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class GoodsController {
    @Autowired
    private DefaultAlipayClient alipayClient;
    HashMap<String,String> map = new HashMap<>();

    @RequestMapping("/sync")
    public HashMap<String,String> sync() throws AlipayApiException {
        AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest();
        request.setBizContent("{" +
                "\"grant_type\":\"authorization_code\"," +
                "\"code\":\"1cc19911172e4f8aaa509c8fb5d12F56\"," +
                "\"refresh_token\":\"201509BBdcba1e3347de4e75ba3fed2c9abebE36\"" +
                "  }");
        AlipayOpenAuthTokenAppResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return map;
    }

    /**
     * 商品文件上传接口
     * @return
     * @throws AlipayApiException
     */
    @RequestMapping("/upload")
    public HashMap<String,String> upload() throws AlipayApiException {
        AlipayMerchantItemFileUploadRequest request = new AlipayMerchantItemFileUploadRequest();
        request.setScene("SYNC_ORDER");
        FileItem FileContent = new FileItem("D:/abc.jpg");
        request.setFileContent(FileContent);
        AlipayMerchantItemFileUploadResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return map;
    }
    @RequestMapping("/create")
    public HashMap<String,String> create() throws AlipayApiException {
        AntMerchantExpandItemOpenCreateRequest request = new AntMerchantExpandItemOpenCreateRequest();
        request.setBizContent("{" +
                "\"scene\":\"APP_ORDER\"," +
                "\"target_id\":\"2018090600502000000022501261\"," +
                "\"target_type\":\"8\"," +
                "\"standard_category_id\":\"2019073100992228000000100000\"," +
                "\"name\":\"西红柿炒蛋\"," +
                "\"description\":\"精选东北优质非转基因大豆\"," +
                "\"type\":\"STANDARD_GOODS\"," +
                "      \"ext_info\":[{" +
                "        \"ext_key\":\"MY_KEY\"," +
                "\"ext_value\":\"MY_VALUE\"" +
                "        }]," +
                "      \"sku_list\":[{" +
                "        \"price\":100," +
                "\"original_price\":100," +
                "\"inventory\":100," +
                "          \"material_list\":[{" +
                "            \"type\":\"ITEM_PIC\"," +
                "\"content\":\"https://xxx\"" +
                "            }]," +
                "          \"property_list\":[{" +
                "            \"property_key\":\"尺寸\"," +
                "\"property_value\":\"大号\"" +
                "            }]" +
                "        }]," +
                "      \"material_list\":[{" +
                "        \"type\":\"ITEM_PIC\"," +
                "\"content\":\"https://xxx\"" +
                "        }]," +
                "      \"property_list\":[{" +
                "        \"property_key\":\"辣度\"," +
                "          \"property_value_list\":[" +
                "            \"特辣\"" +
                "          ]" +
                "        }]" +
                "  }");
        AntMerchantExpandItemOpenCreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return map;
    }
}
